/*
* $Id: QBeeFamilyView.cpp 154 2010-09-27 08:10:48Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "QBeeFamilyView.h"
#include "Components/Journal/FamilyManager.h"
#include "Components/Journal/QueenBeeManager.h"

#pragma warning(disable: 4407)

CQBeeFamilyView::CQBeeFamilyView(CState* pState)
: CQBeeView(pState){}
CQBeeFamilyView::~CQBeeFamilyView(){}

void CQBeeFamilyView::SetState(){
	m_pState->m_nState = Journal::VIEW_FAMILY_QBEE;
}

bool CQBeeFamilyView::IsViewState(State state){
	return state.m_nPosTab == Journal::QUEEN_BEE_JOURNAL &&
		state.m_type == Journal::NONE &&
		state.m_nTypeSel == Journal::POINT;
}

void CQBeeFamilyView::Init(){
	m_infoDrawer.Clear();
	std::map<std::tstring, int> m_aAllQB;
	std::map<std::tstring, int> m_aCurQB;
	std::map<std::tstring, int> m_aIdFamily;
	std::map<int, CFamilyManager::family> aFamilies = IJournalComponent::GetFamilyManager()->GetFamilies();
	for(std::map<int, CFamilyManager::family>::iterator it = aFamilies.begin(); it != aFamilies.end(); it++){
		std::map<std::tstring, int>::iterator itTemp = m_aCurQB.find(it->second.m_strName);
		if(itTemp == m_aCurQB.end())
			m_aCurQB.insert(std::make_pair(it->second.m_strName,  0));
		itTemp = m_aAllQB.find(it->second.m_strName);
		m_aAllQB[it->second.m_strName] = 0;
		m_aIdFamily[it->second.m_strName] = it->first;

		std::map<int, CQueenBeeManager::queen_bee> aQB = IJournalComponent::GetQueenBeeManager()->GetFamilyQB(it->first);
		for(std::map<int, CQueenBeeManager::queen_bee>::iterator it1 = aQB.begin(); it1 != aQB.end(); it1++){
			if(!it1->second.s_bDead)
				m_aCurQB[it->second.m_strName]++;
			m_aAllQB[it->second.m_strName]++;
		}
	}

	for(std::map<std::tstring, int>::iterator it = m_aAllQB.begin(); it != m_aAllQB.end(); it++){
		int j = m_infoDrawer.AddItem(m_aIdFamily[it->first]);
		m_infoDrawer.AddSubItem(j, InfoItem(it->first, _T("Arial"), 20, true, true));
		std::tstring strTemp(ILanguageManager::GetLanguageManager()->GetString(INFO_FAMILY_CUR_COUNT_QB_ID) + _T(" - "));
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + CConvertFunction::ConvertFromInt(m_aCurQB[it->first]), _T("Arial"), 14, false, false));
		strTemp = ILanguageManager::GetLanguageManager()->GetString(INFO_FAMILY_ALL_QB_ID) + _T(" - ");
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + CConvertFunction::ConvertFromInt(it->second), _T("Arial"), 14, false, false));
	}
}

BEGIN_MESSAGE_MAP(CQBeeFamilyView, CQBeeView)
	ON_WM_CREATE()
END_MESSAGE_MAP()

int CQBeeFamilyView::OnCreate(LPCREATESTRUCT lpCreateStruct){
	Init();
	return __super::OnCreate(lpCreateStruct);
}